// 题目链接：https://www.luogu.com.cn/problem/P2385

#include <iostream>
#include <cstring>
#include <queue>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 35;
const int M = 35;

long long dist[M][N];
int g[M][N];
int n, m, m1, m2;
queue<PII> q;

int main()
{
    cin >> m >> n >> m1 >> m2;
    int x1, y1; // 记录起点的坐标
    int x2, y2; // 记录终点的坐标

    for (int i = 1; i <= m; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            cin >> g[i][j];
            if (g[i][j] == 3)
            {
                x1 = i;
                y1 = j;
            }
            if (g[i][j] == 4)
            {
                x2 = i;
                y2 = j;
            }
        }
    }

    int dx[] = {-m1, -m1, m1, m1, -m2, -m2, m2, m2};
    int dy[] = {m2, -m2, m2, -m2, m1, -m1, m1, -m1};

    memset(dist, -1, sizeof dist);

    q.push({x1, y1});
    dist[x1][y1] = 0;

    while (!q.empty())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 8; ++i)
        {
            int next_x = t.x + dx[i];
            int next_y = t.y + dy[i];

            if (next_x <= 0 || next_x > m || next_y <= 0 || next_y > n)
                continue;
            if (g[next_x][next_y] != 1 && g[next_x][next_y] != 4)
                continue;
            if (dist[next_x][next_y] >= 0)
                continue;

            q.push({next_x, next_y});
            dist[next_x][next_y] = dist[t.x][t.y] + 1;

            if (dist[x2][y2] >= 0)
            {
                printf("%lld\n", dist[x2][y2]);
                return 0;
            }
        }
    }

    return 0;
}